#include "copytocomputerbuttonwidget.h"
#include <QVariant>
#include <QDebug>
#include <QFileDialog>
#include <QSettings>
#include <windowmanage.hpp>

const int WIDGET_WIDTH = 134;
const int WIDGET_HEIGHT = 36;

CopyToComputerButtonWidget::CopyToComputerButtonWidget(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(WIDGET_WIDTH, WIDGET_HEIGHT);
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setMargin(0);
    this->setLayout(layout);

    kabase::WindowManage::removeHeader(this);

    QPushButton *copyToComputerButton = new QPushButton();
    copyToComputerButton->setFixedSize(WIDGET_WIDTH, WIDGET_HEIGHT);
    copyToComputerButton->setText(tr("Copy")); // Copy复制到本机
    copyToComputerButton->setProperty("isImportant", true);
    layout->addWidget(copyToComputerButton);

    connect(copyToComputerButton, &QPushButton::clicked, this,
            &CopyToComputerButtonWidget::slotCopyToComputerButtonClicked);
}

CopyToComputerButtonWidget::~CopyToComputerButtonWidget() {}

void CopyToComputerButtonWidget::getSelectDataList(const QList<FileInfo> &nameList)
{
    m_selectDataList = nameList;
}

void CopyToComputerButtonWidget::slotCopyToComputerButtonClicked()
{
    QSettings setting(getenv("HOME") + QString("/.mobilecache/") + QString("setting.ini"), QSettings::IniFormat);
    QString lastPath = setting.value("LastFilePath").toString();
    if (lastPath.isEmpty()) {
        lastPath = getenv("HOME") + QString("/下载");
    }
    QFileDialog fileDialog;
    QString fileName = fileDialog.getExistingDirectory(this, tr("Choose folder"), lastPath); // Choose folder选择文件夹
    if (!fileName.isEmpty()) {
        setting.setValue("LastFilePath", fileName);
        Q_EMIT sigChooseFilePath(fileName, m_selectDataList);
    }
}
